import {
  activityDetails
} from '../../apis/activity';
import {
  imageLiveDetails,
  videoLiveDetails
} from '../../apis/live';
import {
  splicing,
  handleDate
} from '../../utils/util';
import {
  request
} from '../../utils/request';
const app = getApp();

Page({
  data: {
    activityDetails: {},
    navIndex: 0,
    authorizationShow: false,
    domain: request.defaults.editorURL,
    imgLiveDetails: {},
    vdLiveDetails: {}
  },

  onLoad(options) {
    this.setData({
      ...options
    });
  },

  onShow() {
    this.getActivityDetails(this.data.activityId);
  },

  onShareAppMessage() {
    return {
      title: this.data.activityDetails.activityName,
      path: `/pages/activityDetails/index?activityId=${this.data.activityId}`,
      imageUrl: this.data.activityDetails.imageList[0]
    }
  },

  // 获取详情
  getActivityDetails(id) {
    wx.showLoading({
      title: '加载中...',
      mask: true
    });
    activityDetails(id).then(res => {
      wx.hideLoading();
      if (res.data.code === 200) {
        let {
          data
        } = res.data;
        data.imageList = data.imageList.map(v => v = splicing(v));
        data.startTime = handleDate(data.startTime);
        data.closeTime = handleDate(data.closeTime);
        this.setData({
          activityDetails: data
        });
        if (data.liveImgId) {
          this.imgLiveDetails(data.liveImgId);
        };
        if (data.liveVideoId) {
          this.vdLiveDetails(data.liveVideoId);
        };
      };
    });
  },

  // 获取授权信息
  bindgetuserinfo() {
    if (app.globalData.isAuthorization) {
      this.signUpNow();
    } else {
      this.setData({
        authorizationShow: true
      });
    };
  },

  // 轮播图查看
  previewImage(e) {
    let {
      index
    } = e.currentTarget.dataset, list = this.data.activityDetails.imageList;
    wx.previewImage({
      urls: list,
      current: list[index]
    });
  },

  // 介绍
  changeNav(e) {
    let {
      index
    } = e.currentTarget.dataset;
    this.setData({
      navIndex: index
    });
  },

  // 导航
  navigation() {
    wx.openLocation({
      latitude: Number(this.data.activityDetails.lat),
      longitude: Number(this.data.activityDetails.lng),
      address: this.data.activityDetails.address,
      name: this.data.activityDetails.address,
      scale: 18
    });
  },

  // 授权回调
  callBack() {
    this.signUpNow();
  },
  // 立即报名
  signUpNow() {
    if (this.data.activityDetails.activityStatus == 1 && (!this.data.activityDetails.order || (this.data.activityDetails.order && this.data.activityDetails.order.orderStatus == 0))) {
      wx.navigateTo({
        url: `/pages/editUserInfo/index?type=activity&activityId=${this.data.activityId}`
      });
    } else if ((this.data.activityDetails.activityStatus == 1 || this.data.activityDetails.activityStatus == 2 || this.data.activityDetails.activityStatus == 3) && this.data.activityDetails.order && this.data.activityDetails.order.orderStatus != 0) {
      wx.navigateTo({
        url: `/pages/orderDetails/index?orderNo=${this.data.activityDetails.order.orderNo}`
      });
    } else if (this.data.activityDetails.activityStatus == 3 && !this.data.activityDetails.order) {
      wx.showToast({
        title: '您没有报名',
        duration: 1500,
        icon: 'none',
        mask: true
      });
    };
  },

  // 首页
  goHome() {
    wx.switchTab({
      url: '/pages/activity/index'
    });
  },

  // 图片直播
  imgLive() {
    wx.navigateTo({
      url: `/pages/liveDetails/index?liveId=${this.data.activityDetails.liveImgId}`
    });
  },
  imgLiveDetails(id) {
    imageLiveDetails(id).then(res => {
      if (res.data.code === 200) {
        let {
          data
        } = res.data;
        this.setData({
          imgLiveDetails: data
        });
      };
    });
  },
  // 视频直播
  vdLiveDetails(id) {
    videoLiveDetails(id).then(res => {
      if (res.data.code === 200) {
        let {
          data
        } = res.data;
        this.setData({
          vdLiveDetails: data
        });
      };
    });
  },
  vdLive() {

  }
})